<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<#if model??>
<mapper namespace="${model.daoPackageName}.${model.className}Dao">

    <sql id="${model.className}Columns">
    <#list model.columns as column>
    <#if !column_has_next>a.${column.columnName} as ${column.realName}<#else>a.${column.columnName} as ${column.realName},</#if>
    </#list>
    </sql>
    <sql id="${model.className}Where">
    </sql>

    <insert id="insert" parameterType="${model.entityPackageName}.${model.className}" useGeneratedKeys="true" keyProperty="${model.unPrimarykey}">
        insert into ${model.tableName} <trim prefix="(" suffix=")" suffixOverrides="," ><#list model.columns as column>${column.columnName},</#list></trim>
        <trim prefix="values (" suffix=")" suffixOverrides="," ><#list model.columns as column>${"#"}{${column.realName}},</#list></trim>
    </insert>

    <insert id="insertSelective" parameterType="${model.entityPackageName}.${model.className}" useGeneratedKeys="true" keyProperty="${model.unPrimarykey}">
        insert into ${model.tableName} <trim prefix="(" suffix=")" suffixOverrides="," ><#list model.columns as column><if test=" ${column.realName} != null" >${column.columnName},</if></#list></trim>
        <trim prefix="values (" suffix=")" suffixOverrides="," ><#list model.columns as column><if test=" ${column.realName} != null" >${"#"}{${column.realName}},</if></#list></trim>
    </insert>

    <insert id="insertBatch" parameterType="java.util.List">
        insert into ${model.tableName} <trim prefix="(" suffix=")" suffixOverrides="," ><#list model.columns as column>${column.columnName},</#list></trim> values
        <foreach collection="list"  item="obj" separator=",">
            <trim prefix="(" suffix=")" suffixOverrides="," ><#list model.columns as column>${"#"}{obj.${column.realName}},</#list></trim>
        </foreach>
    </insert>


    <update id="updateById" parameterType="${model.entityPackageName}.${model.className}">
        update ${model.tableName} <set><#list model.columns as column>${column.columnName}=${"#"}{${column.realName}},</#list></set> where ${model.primarykey}=${"#"}{id}
    </update>

    <update id="updateSelectiveById" parameterType="${model.entityPackageName}.${model.className}">
        update ${model.tableName} <set><#list model.columns as column><if test="${column.realName} !=null">${column.columnName}=${"#"}{${column.realName}},</if></#list></set>
        where ${model.primarykey}=${"#"}{id}
    </update>

    <update id="updateSelective" >
        update ${model.tableName} <set><#list model.columns as column><if test="entity.${column.realName} !=null">${column.columnName}=${"#"}{entity.${column.realName}},</if></#list></set>
        <where>
            <#list model.columns as column><if test="cond.${column.realName} !=null"> AND ${column.columnName}=${"#"}{cond.${column.realName}}</if></#list>
        </where>
    </update>

    <update id="updateBatchById" >
        <foreach collection="list"  item="obj" separator=";" open="" close="">
            update ${model.tableName} <set><#list model.columns as column><if test="obj.${column.realName} !=null">${column.columnName}=${"#"}{obj.${column.realName}},</if></#list></set> where  ${model.primarykey}=${"#"}{obj.${model.unPrimarykey}}
        </foreach>
    </update>


    <delete id="deleteById" >
        delete from ${model.tableName}  where ${model.primarykey}=${"#"}{id}
    </delete>

    <delete id="deleteOne" parameterType="${model.entityPackageName}.${model.className}">
        delete from ${model.tableName}
        <where>
            <#list model.columns as column><if test="${column.realName} !=null"> AND ${column.columnName}=${"#"}{${column.realName}}</if></#list>
        </where>
    </delete>

    <delete id="deleteByIds">
        delete from ${model.tableName}  where ${model.primarykey} in
        <foreach collection="array" item="item" open="(" close=")" separator=",">${"#"}{item}</foreach>
    </delete>

    <!-- 查询所有数据 -->
    <select id="findAll" resultType="${model.entityPackageName}.${model.className}" parameterType="java.util.Map">
        SELECT <include refid="${model.className}Columns"/> FROM ${model.tableName} a <include refid="${model.className}Where"/>
    </select>

     <select id="findOne" resultType="${model.entityPackageName}.${model.className}" parameterType="${model.entityPackageName}.${model.className}">
        SELECT <include refid="${model.className}Columns"/> FROM ${model.tableName} a
         <where>
             <#list model.columns as column><if test="${column.realName} !=null"> AND a.${column.columnName}=${"#"}{${column.realName}}</if></#list>
         </where>
         limit 1
    </select>
    <select id="findById" resultType="${model.entityPackageName}.${model.className}" >
        SELECT <include refid="${model.className}Columns"/> FROM ${model.tableName} a  where a.${model.primarykey}=${"#"}{id}
    </select>

</mapper>
</#if>